package artid.com.arttogoa;
import artid.com.arttogoa.R;

import artid.com.arttogoa.barcode.Cache;
import artid.com.arttogoa.parser.IbeaconActivityInfoParser;
import artid.com.arttogoa.parser.MyArtPush_IbactivityListBean;
import artid.com.arttogoa.util.ToastUtil;
import artid.com.arttogoa.volley.VolleyBasic;
import artid.com.arttogoa.volley.VolleyCallback;
import artid.com.arttogoa.volley.VolleyData;
import artid.com.arttogoa.volley.VolleyMap;
import com.android.volley.VolleyError;
import com.nostra13.universalimageloader.core.ImageLoader;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;

/**
 * 
 *    
 * 项目名称：ArtworkArtA   
 * 类名称   ：IbeaconActivityInfo_Activity   
 * 类描述   ：Ibeacon活动详情   
 * 创建人   ：耿卫斌   
 * 创建时间：2016-11-10 上午11:01:18   
 *
 */
public class IbeaconActivityInfo_Activity extends BaseActivity implements OnClickListener{
	private int type;//0个人微站，1自定义
	private ImageView headeImage;
	private TextView title_rightBtn,title_tv,url_tv,address_tv,describe_tv,submitBtn;
	private IbeaconActivityInfoParser mIbeaconActivityInfoParser;//活动详情数据
	private MyArtPush_IbactivityListBean mMyArtPush_IbactivityListBean;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_ibeaconactivityinfo);
		initView();
	}
	private void initView(){
		mContext=this;
		setBack();
		setContent(this, getIntent());
		mTittle.setText("绑定详情");
		// 注册当前广播
		IntentFilter filter = new IntentFilter("com.IbeaconActivityInfo_Activity.receiver");
		registerReceiver(broadcastReceiver, filter);
		title_rightBtn=(TextView) findViewById(R.id.IbeaconActivityInfo_title_rightBtn);
		title_rightBtn.setOnClickListener(this);
		headeImage=(ImageView) findViewById(R.id.IbeaconActivityInfo_headeImage);
		title_tv=(TextView) findViewById(R.id.IbeaconActivityInfo_title);
		url_tv=(TextView) findViewById(R.id.IbeaconActivityInfo_url);
		address_tv=(TextView) findViewById(R.id.IbeaconActivityInfo_address);
		describe_tv=(TextView) findViewById(R.id.IbeaconActivityInfo_describe);
		submitBtn=(TextView) findViewById(R.id.IbeaconActivityInfo_submit);
		submitBtn.setOnClickListener(this);
		mIbeaconActivityInfoParser=(IbeaconActivityInfoParser) getIntent().getSerializableExtra("IbeaconActivityInfoParser");
		mMyArtPush_IbactivityListBean=(MyArtPush_IbactivityListBean) getIntent().getSerializableExtra("MyArtPush_IbactivityListBean");
		CustomAssignment();
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		Intent intent;
		switch (v.getId()) {
		case R.id.IbeaconActivityInfo_title_rightBtn:
			//编辑
			intent=new Intent(mContext,BindingIbeacon_Activity.class);
			intent.putExtra("MyArtPush_IbactivityListBean", mMyArtPush_IbactivityListBean);
			intent.putExtra("IbeaconActivityInfoParser", mIbeaconActivityInfoParser);
			intent.putExtra("action", "编辑");
			if("1".equals(mIbeaconActivityInfoParser.getType())){
				//1个人微站，2自定义
				intent.putExtra("type", 0);
			}else if("2".equals(mIbeaconActivityInfoParser.getType())){
				intent.putExtra("type", 1);
			}
			mContext.startActivity(intent);
			break;
		case 	R.id.IbeaconActivityInfo_submit:
			//预览
			intent=new Intent(mContext,BaseWebView_Activity.class);
			intent.putExtra("titleName", "艺推信息");
			intent.putExtra("type", "baseWebView");
			intent.putExtra("webURL", mIbeaconActivityInfoParser.getIbactivityurl());
			startActivity(intent);
			break;

		default:
			break;
		}
	}


	/**
	 * 赋值
	 */
	private void CustomAssignment(){
		ImageLoader.getInstance().displayImage(mIbeaconActivityInfoParser.getIbactivityimage(), headeImage);
		title_tv.setText(mIbeaconActivityInfoParser.getIbactivitytitle());
		url_tv.setText(mIbeaconActivityInfoParser.getIbactivityurl());
		address_tv.setText(mIbeaconActivityInfoParser.getIbactivityaddress());
		describe_tv.setText(mIbeaconActivityInfoParser.getIbactivitycontent());
	}
	
	/**
	 * 获取Ibenacon活动信息
	 */
	private void getActivityInfo(String ibactivityid){
		VolleyBasic.getInstance((Activity)mContext).post(Cache.ibactivityInfo,
				VolleyMap.getInstance().ibactivityInfo(ibactivityid),
				IbeaconActivityInfoParser.class, new VolleyCallback() {

			@SuppressWarnings("unchecked")
			@Override
			public void onSuccess(VolleyData vd) {
				// TODO Auto-generated method stub
				System.out.println(vd.getJson());
				mIbeaconActivityInfoParser=(IbeaconActivityInfoParser) vd.getData();
				CustomAssignment();
				//更新修改后的值
				mMyArtPush_IbactivityListBean.setIbactivityimage(mIbeaconActivityInfoParser.getIbactivityimage());
				mMyArtPush_IbactivityListBean.setIbactivitytitle(mIbeaconActivityInfoParser.getIbactivitytitle());
			}

			public void onError(VolleyError volleyError) {
				ToastUtil.toast(mContext, "操作失败，请重试");
			}
		});
	}
	
	// 对开始接单的广播监听
	BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {

		@Override
		public void onReceive(Context context, Intent intent) {
			// TODO Auto-generated method stub
			if (intent.getBooleanExtra("isRefresh", false) == true) {
				//判断是否是由拍照或者获取本地图片
				getActivityInfo(intent.getStringExtra("ibactivityid"));
			}
		}
	};

	
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		// 关闭广播
		unregisterReceiver(broadcastReceiver);
	}

}
